//
//  DemandHeaderView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/19.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "DemandHeaderView.h"
#import "ReleaseModel.h"
#import "ShowView.h"
#import "UIView+ZF.h"
#import "AppDelegate.h"
@interface DemandHeaderView()<UIScrollViewDelegate,ShowViewDelegate>

@property(nonatomic,strong) UIScrollView *titleSubScrollView;
@property(nonatomic,strong) MCButton *mcBtn;
@property(nonatomic,strong) UIButton *subBtn;
@property(nonatomic,strong) MCButton *preMCBtn;
@property(nonatomic,strong) UIButton *preSubBtn;
@property(nonatomic,strong) UIButton *industryBtn;

@property(nonatomic,assign) CGFloat w;

@property(nonatomic,assign) CGFloat x;
@property(nonatomic,assign) NSInteger maxOffSetX;
@property(nonatomic,assign) NSInteger mcBtnTag;
@property(nonatomic,assign) NSInteger subTag;
@property(nonatomic,assign) NSInteger maxSubBtnX;


@end
@implementation DemandHeaderView
//-(NSMutableArray *)titles
//{
//    if (!_titles) {
//        _titles = [NSMutableArray array];
//    }
//    return _titles;
//}
//-(NSMutableArray *)subTitles
//{
//    if (!_subTitles) {
//        _subTitles = [NSMutableArray array];
//    }
//    return _subTitles;
//}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.subTag = 1;
        self.backgroundColor = kWhite;
        self.mcBtnTag = 1;
        self.titleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 40)];
        self.titleScrollView.showsHorizontalScrollIndicator = NO;
        // 阴影颜色
        self.titleScrollView.layer.shadowColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1].CGColor;
        // 阴影偏移，默认(0, -3)
        self.titleScrollView.layer.shadowOffset = CGSizeMake(-3,0);
        // 阴影透明度，默认0
        self.titleScrollView.layer.shadowOpacity = 0.3;
        // 阴影半径，默认3
        self.industryBtn.layer.shadowRadius = 1;
        self.titleScrollView.delegate = self;
        [self addSubview:self.titleScrollView];
//        [self insertSubview:self.titleScrollView belowSubview:self.industryBtn];
        
       
        self.industryBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth-40, 0, 40, 40)];
        self.industryBtn.backgroundColor = kWhite;
        // 阴影颜色
        self.industryBtn.layer.shadowColor = kOrangeBg.CGColor;
        // 阴影偏移，默认(0, -3)
        self.industryBtn.layer.shadowOffset = CGSizeMake(-2,0);
        // 阴影透明度，默认0
        self.industryBtn.layer.shadowOpacity = 0.3;
        // 阴影半径，默认3
        self.industryBtn.layer.shadowRadius = 1;
        [self addSubview:self.industryBtn];
        [self.industryBtn setImage:[UIImage imageNamed:@"icon_154"] forState:UIControlStateNormal];
        [self.industryBtn addTarget:self action:@selector(clickShowBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    return self;
}
- (void)setTitles:(NSMutableArray *)titles
{
    _titles = titles;
    self.showView = [[ShowView alloc]initWithFrame:CGRectMake(0, 64, kWidth, 0)];
    self.showView.title = @"选择需求";
    self.showView.dataMuta = self.titles;
    self.showView.delegate = self;
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate.window addSubview:self.showView];
    [self setUpUI];
}
-(void)setUpUI
{
    for (int i = 0; i < self.titles.count; i++) {
        ReleaseModel *model = self.titles[i];
        self.w = [MCButton getWidthWithTitle:model.content withFontSize:14]+20;
        //        XLog(@"self.w:%f",self.w);
        self.mcBtn = [[MCButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.mcBtn.frame), 0, self.w, 40)];
        [self.titleScrollView addSubview:self.mcBtn];
        [self.mcBtn setTitle:model.content forState:UIControlStateNormal];
        self.mcBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        self.mcBtn.buttonStyle = imageBottom;
        self.mcBtn.tag = i+1;
        [self.mcBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            [self.mcBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
            //            self.mcBtn.backgroundColor = kRedColor;
            self.preMCBtn = self.mcBtn;
        }else{
            [self.mcBtn setImage:[UIImage new] forState:UIControlStateNormal];
            [self.mcBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
        }
        
        
        
    }
    self.maxOffSetX = CGRectGetMaxX(self.mcBtn.frame)+40;
//    XLog(@"self.maxOffSetX:%ld",(long)self.maxOffSetX);
    self.titleScrollView.contentSize = CGSizeMake(self.maxOffSetX, 0);
}
- (void)setSubTitles:(NSMutableArray *)subTitles
{
    _subTitles = subTitles;
    [self.titleSubScrollView removeFromSuperview];
     [self setUpSubUI];
}
//- (void)setReleaseSubModel:(ReleaseModel *)releaseSubModel
//{
//    _releaseSubModel = releaseSubModel;
//    
//}
-(void)setUpSubUI
{
    
    self.titleSubScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,40, kWidth, 40)];
    self.titleSubScrollView.showsHorizontalScrollIndicator = NO;
    self.titleSubScrollView.delegate = self;
    [self addSubview:self.titleSubScrollView];
    self.titleSubScrollView.backgroundColor = kLabel252Color;
    for (int i = 0; i < self.subTitles.count; i++) {
        ReleaseModel *model = self.subTitles[i];
        self.w = [MCButton getWidthWithTitle:model.content withFontSize:14]+30;
//        XLog(@"self.w:%f",self.w);
        if (i == 0) {
            self.maxSubBtnX = 0;
        }else {
            self.maxSubBtnX = CGRectGetMaxX(self.subBtn.frame);            
        }
        self.subBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.maxSubBtnX+15, 8, self.w, 24)];
        [self.titleSubScrollView addSubview:self.subBtn];
        [self.subBtn setTitle:model.content forState:UIControlStateNormal];
        self.subBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        self.subBtn.layer.masksToBounds = YES;
        self.subBtn.layer.cornerRadius = 3;
//        self.subBtn.layer.borderColor  = kLabel244Color.CGColor;
//        self.subBtn.layer.borderWidth  = 1;

        self.subBtn.tag = i+1;
        [self.subBtn addTarget:self action:@selector(clickSubBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
//            [self.subBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            self.subBtn.backgroundColor = kOrangeBg;
            [self.subBtn setTitleColor:kWhite forState:UIControlStateNormal];
            self.preSubBtn = self.subBtn;
            
        }else{
//            [self.subBtn setImage:[UIImage new] forState:UIControlStateNormal];
            [self.subBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            self.subBtn.backgroundColor = kLabel244Color;
        }
        
        
        
    }
   
    self.titleSubScrollView.contentSize = CGSizeMake(CGRectGetMaxX(self.subBtn.frame)+18, 0);
    
}
-(void)clickBtn:(MCButton *)clickBtn
{
    XLog(@"clickBtn.tag:%ld",clickBtn.tag);
    XLog(@"clickBtn:%@",clickBtn);
    XLog(@"self.preMCBtn:%@",self.preMCBtn);
    if (self.preMCBtn == clickBtn) {
        [self.preMCBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
        [self.preMCBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
    }else {
        [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
        [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        [self.preMCBtn setImage:[UIImage new]  forState:UIControlStateNormal];
        [self.preMCBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
    }
    self.preMCBtn = clickBtn;
    XLog(@"self.titleScrollView.contentOffset.x:%f",self.titleScrollView.contentOffset.x);
    XLog(@"self.maxOffSetX-kWidth/2:%f",(long)self.maxOffSetX-kWidth);
    if (clickBtn.center.x >= kWidth/2) {
        self.x = clickBtn.center.x -kWidth/2;//729-409X
        if (self.x  > CGRectGetMaxX(self.mcBtn.frame)+40-kWidth) {
            [self.titleScrollView setContentOffset:CGPointMake(CGRectGetMaxX(self.mcBtn.frame)+40-kWidth, 0) animated:YES];
        }else {
            
            [self.titleScrollView setContentOffset:CGPointMake(self.x, 0) animated:YES];
        }
    }else {
        [self.titleScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
//    self.subTag = 1;
    self.mcBtnTag = clickBtn.tag;
    
    if ([self.delegate respondsToSelector:@selector(demandHeaderView:withTitleBtn:withSubDefaultTag:)]) {
        [self.delegate demandHeaderView:self withTitleBtn:self.preMCBtn withSubDefaultTag:1];
    }
    XLog(@"clickBtn.tag-1:%ld",clickBtn.tag-1);
    self.showView.defaultRow = clickBtn.tag -1;
    [self.showView hideListViewAnimation];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    XLog(@"scrollView.x:%lf",scrollView.contentOffset.x);//124.5 499.430977
    
}
-(void)clickSubBtn:(MCButton *)clickSubBtn
{
    self.subTag = clickSubBtn.tag;
    if (self.preSubBtn == clickSubBtn) {
        clickSubBtn.backgroundColor = kOrangeBg;
        [clickSubBtn setTitleColor:kWhite forState:UIControlStateNormal];
    }else {
        clickSubBtn.backgroundColor = kOrangeBg;
        [clickSubBtn setTitleColor:kWhite forState:UIControlStateNormal];
        [self.preSubBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
        self.preSubBtn.backgroundColor = kLabel244Color;
    }
    self.preSubBtn = clickSubBtn;
    if (clickSubBtn.center.x >= kWidth/2&&CGRectGetMaxX(self.subBtn.frame)+18>kWidth) {
        self.x = clickSubBtn.center.x -kWidth/2;//729-409X
        if (self.x  > CGRectGetMaxX(self.subBtn.frame)+18-kWidth) {
            [self.titleSubScrollView setContentOffset:CGPointMake(CGRectGetMaxX(self.subBtn.frame)+18-kWidth, 0) animated:YES];
        }else {
        
            [self.titleSubScrollView setContentOffset:CGPointMake(self.x, 0) animated:YES];
        }
    }else {
//        XLog(@"self.x:%lf",self.x);
        [self.titleSubScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    ReleaseModel *subModel = self.subTitles[clickSubBtn.tag-1];
    if ([self.delegate respondsToSelector:@selector(demandHeaderView:withTitleBtn:withSubBtn:WithSubModel:)]) {
        [self.delegate demandHeaderView:self withTitleBtn:self.preMCBtn withSubBtn:clickSubBtn WithSubModel:subModel];
    }
}
#pragma mark-代理:显示遮盖
-(void)clickShowBtn:(UIButton *)clickShowBtn
{
//    clickShowBtn.selected = !clickShowBtn.selected;
//    if (clickShowBtn.selected) {
        self.showView.h = kUIScreen.size.height;
    XLog(@"showView:%@",NSStringFromCGRect(self.showView.frame));
        [self.showView showListViewAnimation];
//    }else {
//        self.showView.h = 0;
//
//        [self.showView hideListViewAnimation];
//    }
}
#pragma mark-代理:隐藏遮盖
-(void)showView:(ShowView *)showView withShow:(BOOL)show
{
//    if (show) {
//        self.showView.h = kUIScreen.size.height;
////        [self.showView showListViewAnimation];
//    }else {
        self.showView.h = 0;
//        [self.showView hideListViewAnimation];
        
//    }
//    self.industryBtn.selected = NO;
}
- (void)showView:(ShowView *)showView withTitle:(NSString *)title withRow:(NSInteger)row withShow:(BOOL)show
{
    
    if (show) {
//        [self.showView removeFromSuperview];
        MCButton *mcBtn = (MCButton *)[self viewWithTag:row];
        
        [self clickBtn:mcBtn];
    }else {
        
    }
//    self.preRow = row;
}
@end
